package tts.controller.users;

import org.springframework.beans.factory.annotation.Autowired;

import tts.controller.RedirectAction;
import tts.dao.UsersHome;
import tts.model.Users;

public class Delete extends RedirectAction{

	//fields
	private static final long serialVersionUID = -1179850978454054762L;
	@Autowired
	private UsersHome usersHome;
	private String username;

	//get & set
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}

	//methods
	public String deleteLogical() {
		try {
			usersHome.delete(this.username);
		} catch (RuntimeException e) {
			addActionError(e.getMessage());
			return ERROR;
		}
		addActionMessage(getText("user.deleted"));
		return SUCCESS;
	}

	public String deletePhisical() {
		try {
			Users u = usersHome.findByUsername(this.username);
			usersHome.deletePhisical(u);
		} catch (RuntimeException e) {
			addActionError(e.getMessage());
			return ERROR;
		}
		addActionMessage(getText("user.deleted"));
		return SUCCESS;
	}

	public String restore() {
		try {
			usersHome.enable(this.username);
		} catch (RuntimeException e) {
			addActionError(e.getMessage());
			return ERROR;
		}
		addActionMessage(getText("user.restored"));
		return SUCCESS;
	}
}
